<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/15
 * Time: 下午11:34
 */

namespace App\Admin\Controllers;

use App\Busi\Traits\CompanyTrait;
use App\Models\Company;
use App\Models\Consult;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;
use App\Busi\Traits\CategoryTrait;
class ConsultController extends Controller
{
    use ModelForm,CategoryTrait,CompanyTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('用户咨询列表');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增用户咨询');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑用户咨询');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(Consult::class, function (Grid $grid) {
            $object= $this;
            $grid->id('ID')->sortable();
            $grid->company_id("分配公司")->display(function ($company_id) use ($object){
                return $object->getCompanyNameByID($company_id);
            });
            $grid->name('姓名');
            $grid->address('地址');
            $grid->phone('电话');


            $grid->subcategory_id('分类名称')->display(function ($category_id) use($object){
                return $object->getFullCategoryNameByID($category_id);
            });
            $grid->status("状态")->display(function($status){
                if( $status==0 ) return '未联系';
                return "已分配";


            });

            $grid->remark("备注");

            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(Consult::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->text('name','姓名');
            $form->text('address','地址');
            $form->text('phone','电话');
            $form->select('company_id',"关联企业")->options(Company::pluck("name","id"));
            $form->radio('status','状态')->options([0=>"未联系",1=>"已分配"]);
            $form->select('supcategory_id',"一级类目")->options(SupQualifyCategory::pluck("category_name","id"))->load('subcategory_id', '/api/admin_subcategory');
            $form->select("subcategory_id","二级类目")->options(function (){
                $supcategory_id = $this->supcategory_id;
                return SecQualifyCategory::where("parent_cate_id",$supcategory_id)->pluck('category_name','id');
            });
            $form->text('remark','备注');
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是']);
        });
    }

}